package com.ecloud.service.translate.service.impl;

import java.time.LocalDateTime;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecloud.common.base.result.Result;
import com.ecloud.common.base.result.ResultBuilder;
import com.ecloud.common.base.result.ResultStatus;
import com.ecloud.common.base.result.Rows;
import com.ecloud.common.base.result.RowsBuilder;
import com.ecloud.openapi.translate.convert.BaseDictionaryConvert;
import com.ecloud.openapi.translate.dto.BaseDictionaryDto;
import com.ecloud.openapi.translate.po.BaseDictionaryPo;
import com.ecloud.openapi.translate.vo.BaseDictionaryListVo;
import com.ecloud.openapi.translate.vo.BaseDictionaryVo;
import com.ecloud.service.translate.dao.BaseDictionaryDao;
import com.ecloud.service.translate.service.iface.IBaseDictionaryService;

/**
 * 基础字典表 服务实现类
 *
 * @author LiuGangQiang Create in 2021/08/06
 */
@Service
public class BaseDictionaryService extends ServiceImpl<BaseDictionaryDao, BaseDictionaryPo>
        implements IBaseDictionaryService {

    @Override
    public Result<?> insert(BaseDictionaryDto dto) {
        /* 第一步:校验数据是否冲突 */
        LambdaQueryWrapper<BaseDictionaryPo> conflictWrapper = new LambdaQueryWrapper<>();
        conflictWrapper.select(BaseDictionaryPo::getId);
        conflictWrapper.eq(BaseDictionaryPo::getCode, dto.getCode());
        conflictWrapper.eq(BaseDictionaryPo::getEnable, true);
        BaseDictionaryPo conflict = baseMapper.selectOne(conflictWrapper);
        if (conflict != null) {
            /* 存在冲突则返回提示 */
            return ResultBuilder.build(ResultStatus.CONFLICT);
        }

        /* 第二步:设置默认信息并新增 */
        BaseDictionaryPo baseDictionaryPo = BaseDictionaryConvert.INSTANCE.dtoToPo(dto);
        baseDictionaryPo.setUpdateTime(LocalDateTime.now());
        baseDictionaryPo.setCreateTime(LocalDateTime.now());
        if (baseMapper.insert(baseDictionaryPo) > 0) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    @Override
    public Result<?> delete(Long id) {
        /* 第一步:检查数据是否存在 */
        LambdaQueryWrapper<BaseDictionaryPo> existsWrapper = new LambdaQueryWrapper<>();
        existsWrapper.select(BaseDictionaryPo::getId);
        existsWrapper.eq(BaseDictionaryPo::getId, id);
        BaseDictionaryPo exists = baseMapper.selectOne(existsWrapper);
        if (exists == null) {
            /* 不存在则返回提示 */
            return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND);
        }

        /* 第二步:存在则执行删除 */
        if (baseMapper.deleteById(id) > 0) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    @Override
    public Result<?> update(BaseDictionaryDto dto) {
        /* 第一步:检查数据是否存在 */
        LambdaQueryWrapper<BaseDictionaryPo> existsWrapper = new LambdaQueryWrapper<>();
        existsWrapper.select(BaseDictionaryPo::getId);
        existsWrapper.eq(BaseDictionaryPo::getId, dto.getId());
        BaseDictionaryPo exists = baseMapper.selectOne(existsWrapper);
        if (exists == null) {
            /* 不存在则返回提示 */
            return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND);
        }

        /* 第二步:检查数据是否冲突 */
        LambdaQueryWrapper<BaseDictionaryPo> conflictWrapper = new LambdaQueryWrapper<>();
        conflictWrapper.select(BaseDictionaryPo::getId);
        conflictWrapper.ne(BaseDictionaryPo::getId, dto.getId());
        conflictWrapper.eq(BaseDictionaryPo::getCode, dto.getCode());
        conflictWrapper.eq(BaseDictionaryPo::getEnable, true);
        BaseDictionaryPo conflict = baseMapper.selectOne(conflictWrapper);
        if (conflict != null) {
            /* 存在冲突则返回提示 */
            return ResultBuilder.build(ResultStatus.CONFLICT);
        }
        /* 第三步:满足条件修改数据 */
        dto.setUpdateTime(LocalDateTime.now());
        if (baseMapper.updateByDto(dto) > 0) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    @Override
    @Cacheable(value = "dict", unless = "#result.code != 200")
    public Result<Rows<BaseDictionaryListVo>> queryList(BaseDictionaryDto dto) {
        IPage<BaseDictionaryListVo> page = baseMapper.queryList(new Page<>(dto.getPage(), dto.getRows()), dto);
        if (page.getTotal() > 0) {
            /* 转换数据为VO */
            return ResultBuilder.build(ResultStatus.OK, RowsBuilder.build(page.getTotal(), page.getRecords()));
        }
        return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND);
    }

    @Override
    public Result<?> queryDetail(Long id) {
        BaseDictionaryVo vo = baseMapper.queryOne(id);
        if (vo != null) {
            /* 转换数据为VO */
            return ResultBuilder.build(ResultStatus.OK, vo);
        }
        return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND);
    }

}
